const express = require('express');
var router = express.Router();
// eslint-disable-next-line no-unused-vars
const tools = require('../../model/tools')
const FocusModel = require('../../model/focusModel')
const ManagerModel = require('../../model/managerModel')
const NavModel = require('../../model/navModel')
const ArticleCateModel = require("../../model/articleCateModel");
const ArticleModel = require("../../model/articleModel");
//配置所有model
var appModel ={
    FocusModel:FocusModel,
    ManagerModel:ManagerModel,
    NavModel:NavModel,
    ArticleCateModel:ArticleCateModel,
    ArticleModel:ArticleModel
}

router.get("/",(req,res)=>{
    res.render("admin/main/index.html");
})

router.get("/welcome",(req,res)=>{
   res.send('欢迎来到后台')
})

router.get("/changeStatus",(req,res)=>{
   let {id,field} = req.query;
   let model = req.query.model +"Model";//他是字符串不是变量,要让他变成变量,借助对象的属性
    appModel[model].find({"_id":id},(err,data)=>{
        if(err){
            console.log(err);
            return;
        }
        if(data.length>0){//如果有数据
            let json;//要更新的数据
            let tempFiled = data[0][field];//获取要修改的字段
            tempFiled == 1?json = {[field]:0} : json = {[field]:1};
            appModel[model].updateOne({"_id":id},json,(err)=>{
                if(err){
                    console.log(err);
                    return;
                }       
                res.send({code:1,msg:"修改成功"});
            })
        }else{
            res.send({code:0,msg:"修改失败"});
        }
    })

 })
 router.get("/changeNum",(req,res)=>{
    let {id,field,num} = req.query;
    let model = req.query.model +"Model";//他是字符串不是变量,要让他变成变量,借助对象的属性
        appModel[model].find({"_id":id},(err,data)=>{
            if(err){
                console.log(err);
                return;
            }
            var result = data;
            if(data.length>0){//如果有数据
                let json ={[field]:num};//要更新的数据
                appModel[model].updateOne({"_id":id},json,(err)=>{
                    if(err){
                        console.log(err);
                        return;
                    }       
                    res.send({code:1,msg:"修改成功",sort:result[0].sort});
                })
            }else{
                res.send({code:0,msg:"修改失败"});
            }
        })
  })
module.exports = router